home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / System / Chooser / CloakShare / CloakShare.c < prev    next >
C/C++ Source or Header  |  1993-06-18  |  3KB  |  158 lines

  1. /*
  2.     CloakShare
  3.     
  4.     By Eric Traut & Dan Clifford
  5.     June 17, 1993
  6.     
  7.     See accompanying ReadMe file for more info
  8. */
  9.  
  10. #include <Types.h>
  11. #include <SetUpA4.h>
  12. #include <OSUtils.h>
  13. #include <Files.h>
  14. #include <Traps.h>
  15. #include <AppleTalk.h>
  16.  
  17. #define kPBControlTrap 0xA004
  18.  
  19. #define PushRegs()    asm { movem.l a0-a1/a4/d0-d1,-(sp)  };
  20. #define PopRegs()    asm { movem.l (sp)+, a0-a1/a4/d0-d1 };
  21.  
  22. typedef pascal OSErr (* PBRoutinePtr)(CntrlParam *someParamBlock);
  23.  
  24. /* global variables accessed via THINK’s A4 world */
  25.  
  26. static PBRoutinePtr OriginalPBControl;
  27. static char fileShareName[] = "\pAFPServer";
  28. static char newFileShareName[] = "\pAFPSërver";
  29. static char matchFileShareName[] = "\pAFPS≈rver";
  30.  
  31. void HandleA4ForFutureCalls(Boolean save, Boolean restore)
  32. {
  33.     if (save)
  34.         goto saveAsm;
  35.     if (restore)
  36.         goto restoreAsm;
  37.     
  38.     end: return;
  39.     
  40.     asm {
  41.         @1     dc.l 1
  42.         saveAsm :
  43.             lea    @1,a1
  44.             move.l a4,(a1)
  45.             jmp @end
  46.         restoreAsm :
  47.             lea    @1,a4
  48.             move.l (a4),a4
  49.             jmp @end    
  50.     }
  51. }
  52.  
  53.  
  54. Boolean ComparePStrings(char *first, char *second)
  55. {
  56.     int count;
  57.     if (first[0] != second[0]) return false;
  58.     for (count=1;count<=first[0];count++)
  59.         if (first[count] != second[count]) return false;
  60.     return true;
  61. }
  62.  
  63. void AlterPString(char *theString)
  64. {
  65.     int count;
  66.     for (count=1;count<=newFileShareName[0];count++)
  67.         theString[count] = newFileShareName[count];
  68. }
  69.  
  70. void AlterPStringToWildCard(char *theString)
  71.  
  72. {
  73.     int count;
  74.     for (count=1;count<=newFileShareName[0];count++)
  75.         theString[count] = matchFileShareName[count];
  76. }
  77.  
  78. #pragma parameter __D0 PBControlIntercept(__A0)
  79. pascal OSErr PBControlIntercept(CntrlParam *someParamBlock)
  80. {
  81.     register CntrlParam *theParamBlock;
  82.     register short trapNumber;
  83.     register char *typeName;
  84.  
  85.     asm {     
  86.         move.l a0, theParamBlock            // Copy A0 into a place the compiler knows about
  87.         move.w d0, trapNumber
  88.  
  89.         pea @returnAddress                    // Push address for return from real handler
  90.         clr.l -(sp)                         // Push space for the real handler address
  91.     }
  92.     
  93.     PushRegs();                                // Save any temp regs we might use in C code,
  94.                                             // and save A4.
  95.  
  96.     HandleA4ForFutureCalls(false,true);
  97.  
  98.     asm { move.l OriginalPBControl, 20(sp) }
  99.  
  100.     if (theParamBlock) {
  101.         if (theParamBlock->ioCRefNum == -10) {
  102.             if (theParamBlock->csCode == lookupName) {
  103.                 typeName = (char*)*(long *)(&(theParamBlock->csParam[1]));
  104.                 if (typeName) {
  105.                     typeName += typeName[0] + 1;
  106.                     if (ComparePStrings(fileShareName,typeName)) {
  107.                         AlterPStringToWildCard(typeName);    
  108.                     }
  109.                 }
  110.             }
  111.             if (theParamBlock->csCode == registerName) {
  112.                 typeName = (char*)*(long *)(&(theParamBlock->csParam[1]));
  113.                 if (typeName) {
  114.                     typeName += 9;
  115.                     typeName += typeName[0] + 1;
  116.                     if (ComparePStrings(fileShareName,typeName)) {
  117.                         AlterPString(typeName);
  118.                     }
  119.                 }
  120.             }
  121.         } 
  122.     }        
  123.  
  124.     PopRegs();                                // Restore temp regs we might have used in C
  125.                                             // code, meanwhile restoring A4.
  126.     
  127.     asm { rts }
  128.     
  129. returnAddress:;
  130.     
  131.     asm { 
  132.         movem.l (sp)+, a2/a3/d7
  133.         unlk a6
  134.         rts 
  135.     }
  136.  
  137. pascal main()
  138. {
  139.     
  140.     RememberA0();                        /* Use Think C's A4 world stuff to set up our A4. */
  141.     SetUpA4();
  142.     
  143.     HandleA4ForFutureCalls(true,false);    /* Save the A4 in our own code so we can use it in */
  144.                                         /* trap calls. */
  145.                                         
  146.     /* Get the addresses for all relevant traps, patch them through out interceptor. */
  147.     /* Remember their addresses for chaining, too. */
  148.         
  149.     OriginalPBControl = (PBRoutinePtr)(NGetTrapAddress(kPBControlTrap, 0));
  150.     NSetTrapAddress((long)&PBControlIntercept, kPBControlTrap, 0);
  151.     
  152.     RestoreA4();    
  153. }
  154.  
  155.  
  156.  
  157.